#include "PusherStepCalculator.h"
#include "MoveAbilityVisitor.h"
#include "ActivateAbilityVisitor.h"
#include "ActivateStep.h"


PusherStepCalculator::PusherStepCalculator(): activatableObject(0), activatableInterface(0)
{
}

PusherStepCalculator::~PusherStepCalculator()
{
	Clear();
}

void PusherStepCalculator::Clear()
{
	moveStepCalculator.Clear();

	std::list<Step*>::iterator it;

	for(it = steps.begin(); it != steps.end(); ++it)
	{
		delete (*it);
	}

	steps.clear();
}

void PusherStepCalculator::SetActivator(GameObject* pusher)
{
	ActivateAbilityVisitor activateVisitor;

	pusher->Accept(activateVisitor);

	if (activateVisitor.FoundAbility())
	{
		activatableObject = pusher;
		activatableInterface = activateVisitor.ActivatableInterface();
	}
}

void PusherStepCalculator::SetPushDirection(const Direction& direction)
{
	pushDirection = direction;
}

void PusherStepCalculator::GenerateSteps(Board& board)
{
	Clear();

	if (!activatableObject && !activatableInterface)
		return;

	Cell& cell = board.CellAt(activatableObject);

	moveStepCalculator.SetInitialCell(cell, pushDirection);
	moveStepCalculator.GenerateSteps(board);

	if (!moveStepCalculator.IsEmpty())
	{
		ActivateStep* step = new ActivateStep();

		step->SetObject(activatableObject, activatableInterface);
		steps.push_front(step);
	}
}


void PusherStepCalculator::Execute()
{
	std::list<Step*>::iterator it;

	for(it = steps.begin(); it != steps.end(); ++it)
	{
		(*it)->Execute();
	}

	moveStepCalculator.Execute();
}


